package demo.module_login.m;

import org.json.JSONObject;

import demo.module_login.contract.ILoginContract;
import demo.module_login.m.data.UserInfo;
import demo.module_login.net.ILoginApi;
import library.base.mvp.module.BaseModule;
import library.net.RxRetrofitUtils;
import library.net.retrofit.exception.RxException;
import library.net.retrofit.exception.RxExceptionHandler;
import library.net.retrofit.observer.RxObserver;
import module.common.net.Schedulers;

/**
 * @author Created by qiang.hou on 2018/11/30.
 * @version 1.0
 */
public class LoginModule extends BaseModule implements ILoginContract.IModel {

    @Override
    public void loginByPassword(String userName, String password, final IModelObserver<UserInfo, RxException> responseObserver) {
        RxRetrofitUtils.getInstance()
                .create(ILoginApi.class)
                .loginByPassword(userName, password)
                .compose(Schedulers.<UserInfo>response_data2())
                .compose(Schedulers.<UserInfo>io_main())
                .subscribe(new RxObserver<UserInfo>() {
                    @Override
                    public void onNext(UserInfo userInfo) {
                        notifyModel(userInfo, responseObserver);
                    }

                    @Override
                    protected void doError(Throwable e) {
                        notifyError(RxExceptionHandler.handleException(e), responseObserver);
                    }
                });
    }

    @Override
    public void loginByCode(String phone, String code, final IModelObserver<UserInfo, RxException> responseObserver) {
        RxRetrofitUtils.getInstance()
                .create(ILoginApi.class)
                .loginByCode(phone, code)
                .compose(Schedulers.<UserInfo>response_data2())
                .compose(Schedulers.<UserInfo>io_main())
                .subscribe(new RxObserver<UserInfo>() {
                    @Override
                    public void onNext(UserInfo userInfo) {
                        notifyModel(userInfo, responseObserver);
                    }

                    @Override
                    protected void doError(Throwable e) {
                        notifyError(RxExceptionHandler.handleException(e), responseObserver);
                    }
                });
    }

    @Override
    public void getSmsCode(String phoneNumber, final IModelObserver<Object, RxException> responseObserver) {
        RxRetrofitUtils.getInstance()
                .create(ILoginApi.class)
                .verificationCode(phoneNumber)
                .compose(Schedulers.<Object>response_data2())
                .compose(Schedulers.<Object>io_main())
                .subscribe(new RxObserver<Object>() {
                    @Override
                    public void onNext(Object object) {
                        notifyModel(object, responseObserver);
                    }

                    @Override
                    protected void doError(Throwable e) {
                        notifyError(RxExceptionHandler.handleException(e), responseObserver);
                    }
                });
    }

    @Override
    public void checkRegister(String phone, final IModelObserver<JSONObject, RxException> responseObserver) {
        RxRetrofitUtils.getInstance()
                .create(ILoginApi.class)
                .checkPhoneIfRegistered(phone)
                .compose(Schedulers.<JSONObject>response_data2())
                .compose(Schedulers.<JSONObject>io_main())
                .subscribe(new RxObserver<JSONObject>() {
                    @Override
                    public void onNext(JSONObject object) {
                        notifyModel(object, responseObserver);
                    }

                    @Override
                    protected void doError(Throwable e) {
                        notifyError(RxExceptionHandler.handleException(e), responseObserver);
                    }
                });
    }
}
